package com.prs.crm.action.oa;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.CarPermission;
import com.prs.crm.domain.oa.Vehicle;
import com.prs.crm.domain.oa.VehicleApplications;
import com.prs.crm.service.oa.CarPermissionService;
import com.prs.crm.service.oa.VehicleApplicationsService;
import com.prs.crm.service.oa.VehicleService;

public class VehicleApplicationsAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private VehicleApplications vehicleApplications;
	private VehicleApplicationsService vehicleApplicationsService;
	private List<Vehicle> vehicles;
	private VehicleService vehicleService;
	private Map<Vehicle, List<VehicleApplications>> map;
	private String Times;
	private String state;
	private Integer[] ids;
	private CarPermissionService carPermissionService;
	@Action("editVehicleApplications")
	public String editVehicleApplications() {
		int yyyy, MM, dd;
		Calendar c = Calendar.getInstance();
		yyyy = c.get(Calendar.YEAR);
		MM = c.get(Calendar.MONTH) + 1;
		dd = c.get(Calendar.DAY_OF_MONTH);
		this.Times = yyyy + "-" + MM + "-" + dd;
		this.vehicles=this.vehicleService.getVehicleAll();
		if(vehicles!=null){
			map=new HashMap<Vehicle, List<VehicleApplications>>();
			for(int i=0;i<vehicles.size();i++){
				Vehicle v=vehicles.get(i);
				List<VehicleApplications> apps=this.vehicleApplicationsService.getAllVehicleApplications();
				map.put(v, apps);
			}
		}	
		return SUCCESS;
	}
	@Action("listVehicleApplications")
	public String listVehicleApplications() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				VehicleApplications.class).add(Restrictions.eq("recorder.id", AppContext.getAccountContext().getUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.vehicleApplicationsService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("listDistributecarManager")
	public String listDistributecarManager(){
		Person currPerson=AppContext.getAccountContext().currentUser();
		List<VehicleApplications> va=this.getVehicleApplicationsService().getAllVehicleApplications();
		for (VehicleApplications ob : va) {
//			System.out.println(ob.getCarFor());
			if (ob.getState().equals("已派车")) {
				if (ob.getEndTime().before(new Date())) {
					ob.setState("已派车(系统结束)");
					this.getVehicleApplicationsService().submit(ob);
				}
			}
			
			CarPermission ca=this.getCarPermissionService().getCarPermissionByDept(ob.getVehicle().getDept().getId());
			if (ca!=null) {
				String[] per=ca.getSelectedId().split(",");
				for (String pers : per) {
					if (currPerson.getId().equals(Integer.parseInt(pers))) {
						ob.setShowState(currPerson.getId()+":1");
						this.getVehicleApplicationsService().submit(ob);
						break;
					}else{
						ob.setShowState("不能查看");
						this.getVehicleApplicationsService().submit(ob);
					}
				}
			}else{
				ob.setShowState(currPerson.getId()+":1");
				this.getVehicleApplicationsService().submit(ob);
			}
		}
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				VehicleApplications.class);
		criteria.add(Restrictions.eq("showState", currPerson.getId()+":1"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.vehicleApplicationsService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listHistory")
	public String listHistory(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				VehicleApplications.class).add(Restrictions.eq("remark", "派车"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.vehicleApplicationsService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editDistributecarManager")
	public String editDistributecarManager(){
		this.editVehicleApplications();
		return SUCCESS;
	}

	@Action("editDistributecarPrint")
	public String editDistributecarPrint(){
		this.editVehicleApplications();
		return SUCCESS;
	}
	@Action(value = "print", results = {
			@Result(name = "success", type = "redirect", location = "editDistributecarPrint?vehicleApplications=${vehicleApplications.getId()}"),
			@Result(name = "error", type = "chain", location = "listDistributecarManager"),
		 })
    public String print(){
		if(ids!=null){
			this.vehicleApplications = this.getVehicleApplicationsService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action("editUseCarInfo")
	public String editUseCarInfo(){
		this.editVehicleApplications();
		return SUCCESS;
	}
	@Action(value = "overUseCar", results = {
			@Result(name = "success", type = "redirect", location = "editOverUseCar?vehicleApplications=${vehicleApplications.getId()}"),
			@Result(name = "error", type = "chain", location = "listDistributecarManager"),
		 })
    public String overUseCar(){
		if(ids!=null){
			this.vehicleApplications = this.getVehicleApplicationsService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action("editOverUseCar")
	public String editOverUseCar(){
		this.editVehicleApplications();
		return SUCCESS;
	}
	@Action("editOverUseCars")
	public String editOverUseCars(){
			this.editVehicleApplications();
		return SUCCESS;
	}
	@Action("editUpdateCar")
	public String editUpdateCar(){
		this.editVehicleApplications();
		return SUCCESS;
	}
	@Action(value = "arrangeCar", results = {
			@Result(name = "success", type = "redirect", location = "listDistributecarManager"),
			@Result(name = "error", type = "chain", location = "listDistributecarManager"),
		 })
	public String arrangeCar(){
		if(ids!=null){
			this.vehicleApplications = this.getVehicleApplicationsService().get(ids[0]);
		}
		vehicleApplications.setState("已派车");
		this.getVehicleApplicationsService().save(vehicleApplications);
		return SUCCESS;
	}
	@Action(value = "saveHistory", results = {
			@Result(name = "success", type = "redirect", location = "listHistory"),
			@Result(name = "error", type = "chain", location = "editRecord"),
		 })
	public String saveHistory(){
		if("".equals(vehicleApplications.getVehicle().getDriver())){
			this.addActionError("驾驶员不能为空！");
			return ERROR;
		}
		this.getVehicleApplicationsService().save(vehicleApplications);
		return SUCCESS;
	}
	@Action("editRecord")
	public String editRecord(){
		System.out.println(vehicleApplications.getId());
		System.out.println(vehicleApplications.getVehicle().getDriver());
		return SUCCESS;
	}
	@Action(value = "alterRecord", results = {
			@Result(name = "success", type = "redirect", location = "editRecord?vehicleApplications=${vehicleApplications.getId()}"),
			@Result(name = "error", type = "chain", location = "listHistory"),
		 })
	public String alterRecord(){
		if(ids!=null){
			this.vehicleApplications  = this.getVehicleApplicationsService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value = "saveSendApply", results = {
			@Result(name = "success", type = "redirect", location = "listVehicleApplications"),
			@Result(name = "error", type = "chain", location = "editVehicleApplications"),
		 })
	public String saveSendApply(){
		vehicleApplications.setState("等待审批派车");
		System.out.println(vehicleApplications.getVehicle().getDept().getName());
		System.out.println(vehicleApplications.getVehicle().getDept().getName());
		if(AppContext.getAccountContext().getUser().getDept().getName().equals(vehicleApplications.getVehicle().getDept().getName())){
			vehicleApplications.setRemark("派车");
		}
		this.getVehicleApplicationsService().save(vehicleApplications);
		return SUCCESS;
	}
	
	@Action(value = "saveUpdateCar", results = {
			@Result(name = "success", type = "redirect", location = "listDistributecarManager"),
			@Result(name = "error", type = "chain", location = "editUpdateCar"),
		 })
	public String saveUpdateCar(){
		this.getVehicleApplicationsService().save(vehicleApplications);
		return SUCCESS;
	}
	
	@Action(value = "saveVehicleApplications", results = {
			@Result(name = "success", type = "redirect", location = "listVehicleApplications"),
			@Result(name = "error", type = "chain", location = "editVehicleApplications"),
		 })
	public String saveVehicleApplications(){
		vehicleApplications.setState("暂存申请");
		this.getVehicleApplicationsService().save(vehicleApplications);
		return SUCCESS;
	}
	
	@Action(value = "saveDirectCar", results = {
			@Result(name = "success", type = "redirect", location = "listDistributecarManager"),
			@Result(name = "error", type = "chain", location = "editDistributecarManager"),
		 })
	public String saveDirectCar(){
		vehicleApplications.setState("已派车");
		this.getVehicleApplicationsService().save(vehicleApplications);
		return SUCCESS;
	}
	@Action(value = "saveOverUseCar", results = {
			@Result(name = "success", type = "redirect", location = "listDistributecarManager"),
			@Result(name = "error", type = "chain", location = "editOverUseCar"),
		 })
	public String saveOverUseCar(){
		vehicleApplications.setState("已结束");
		this.getVehicleApplicationsService().save(vehicleApplications);
		return SUCCESS;
	}
	@Action(value = "removeVehicleApplications", results = {
			@Result(name = "success", type = "redirect", location = "listVehicleApplications"),
			@Result(name = "error", type = "chain", location = "listVehicleApplications"),
		 })
	public String removeVehicleApplications(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
//		if(state=="暂存申请"){
//			this.addActionError("存在非暂存车辆信息，不能删除！");
//			return ERROR;
//		}
		this.getVehicleApplicationsService().batchRemove(ids);
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public VehicleApplications getVehicleApplications() {
		return vehicleApplications;
	}
	public void setVehicleApplications(VehicleApplications vehicleApplications) {
		this.vehicleApplications = vehicleApplications;
	}
	public VehicleApplicationsService getVehicleApplicationsService() {
		return vehicleApplicationsService;
	}
	public void setVehicleApplicationsService(
			VehicleApplicationsService vehicleApplicationsService) {
		this.vehicleApplicationsService = vehicleApplicationsService;
	}

	public List<Vehicle> getVehicles() {
		return vehicles;
	}

	public void setVehicles(List<Vehicle> vehicles) {
		this.vehicles = vehicles;
	}

	public VehicleService getVehicleService() {
		return vehicleService;
	}

	public void setVehicleService(VehicleService vehicleService) {
		this.vehicleService = vehicleService;
	}
	public Map<Vehicle, List<VehicleApplications>> getMap() {
		return map;
	}
	public void setMap(Map<Vehicle, List<VehicleApplications>> map) {
		this.map = map;
	}
	public String getTimes() {
		return Times;
	}
	public void setTimes(String times) {
		Times = times;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public CarPermissionService getCarPermissionService() {
		return carPermissionService;
	}
	public void setCarPermissionService(CarPermissionService carPermissionService) {
		this.carPermissionService = carPermissionService;
	}
	
}
